﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    PM.PMBasic.Mail amail = new PM.PMBasic.Mail();
    protected void Page_Load(object sender, EventArgs e)
    {

        Session["tid"] = "admin";
        Session["name"] = "章子怡";
        Session["sex"] = "女";
        Session["depart"] = "信息工程学院";
        Session["usertype"] = "4";

        if (Session["sex"] == null || Session["tid"] == null)
        {
            PM.Code.Web.alertmessage.ShowMsg("账号登录已超时，请返回重新登录。", "Index.aspx");
            return;
        }

        if (!IsPostBack)
        {
            ShowPanel();
            changestatus();
            dlDatabind();
        } 
    }
    //显示用户信息
    protected void ShowPanel()
    {
        if (Session["sex"].ToString() == "女")
            Image1.ImageUrl = "~/Images/f" + Session["usertype"].ToString() + ".png";
        else Image1.ImageUrl = "~/Images/m" + Session["usertype"].ToString() + ".png";
        lblname.Text = Session["name"].ToString();
        lbldepart.Text = Session["depart"].ToString();
        switch (Session["usertype"].ToString())
        {
            case "1": lblUsertype.Text = "普通教师"; break;
            case "2": lblUsertype.Text = "学院管理员"; break;
            case "3": lblUsertype.Text = "项目管理员"; break;
            case "4": lblUsertype.Text = "系统管理员"; break;
            default: lblUsertype.Text = "未知权限"; break;
        }
        int a=amail.GetUnread(Session["tid"].ToString());
        if (a>0)
        {
            lblmail.Text=a.ToString();
            lblmail.ForeColor = System.Drawing.Color.Red;
            imgmail.ImageUrl = "~/images/Mail New.png";
        }
    }


    //修改入口开启与关闭的状态
    protected void changestatus()
    {
        //构建SQL语句LIST
        List<string> sqlstr = new List<string>();
        string now=DateTime.Now.ToString();
        string sql1 = "update Sys_Guide set btnstate='on' where  id<>14 and id in(select distinct type from Bus_ProjectPlan where opendate<'" + now + "' and closedate>'" + now + "' and pstatus='已完成')";
        string sql2 = "update Sys_Guide set btnstate='off' where id<>14 and id not in(select distinct type from Bus_ProjectPlan where opendate<'" + now + "' and closedate>'" + now + "'and pstatus='已完成')";
        sqlstr.Add(sql2);
        sqlstr.Add(sql1);
        PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstr);
    }

    //控制按钮灯的开关显示效果
    protected void dlDatabind()
    {
        StringBuilder sb=new StringBuilder();
        sb.Append("select id,btnname,case btnstate when 'on' then btnurl0 else btnurl1 end as btnurl,btnstate from Sys_Guide order by id");
        DataTable dt=PM.Code.DAL.DbHelper.ExecuteDataTableBySql(sb.ToString());
        dlInterface.DataSource = dt;
        dlInterface.DataBind();
        
    }
    //页面导航操作
    protected void dlInterface_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "imgBtnclick")
        {
            Label lblBtnstate = (Label)e.Item.FindControl("lblBtnstate");
            Label lblId = (Label)e.Item.FindControl("lblId");
            if (lblBtnstate.Text.Trim().Equals("admin"))
            {
                Response.Redirect("web/Default.aspx?id=99", true);
            } 
            else
            {
                Response.Redirect("web/Default.aspx?id=" + lblId.Text.Trim(), true);
            }  
        } 
        else
        {
            //这里会有else么？貌似不会……先空着吧^_^~愉快地coding……
        }
    }
    //禁用状态为关的按钮
    protected void dlInterface_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label lblBtnstate = (Label)e.Item.FindControl("lblBtnstate");
        ImageButton imgBtn = (ImageButton)e.Item.FindControl("imgBtn");
        if (lblBtnstate.Text.Trim().Equals("off"))
        {
            imgBtn.Enabled = false;
        }
        else
        {
            imgBtn.Enabled =true;
        }
    }
    protected void btnzhuxiao_Click(object sender, ImageClickEventArgs e)
    {
        Session.RemoveAll();
        Response.Redirect("Index.aspx");
    }
}

